﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{

    public class EquipRemodelPage : Page
    {
        public GameObject leftRoot;
        public GameObject rightRoot;

        public UILabel leftEquipName;
        public UILabel rightEquipName;

        public UIToggle checkBox;

        public GameObject particlePref;
        public GameObject particleObj;
        public GameObject frameRoot;
        public AloneTextureKit equipBefore;
        public AloneTextureKit equipAfter;

        string heroIdBefore;
        string heroIdAfter;
        
        EquipIcon leftIcon;
        EquipIcon rightIcon;

        /// <summary>
        /// 要装的装备
        /// </summary>
        string leftGuid;

        /// <summary>
        /// 要装在哪个位置的人身上，再判断是否是与此人匹配的装备
        /// </summary>
        int heroPosition;

        public void OnConfirmBtn()
        {
            CSFormationOn msg = new CSFormationOn();
            UIEquipment equip = StaticData.playerData.GetUIEquipment(leftGuid);
            msg.guid = leftGuid;
            msg.id = equip.equipId;
            msg.formatPos = heroPosition;
            NetAPI.SendHttp(OpDefine.CSFormationOn, msg, EquipCallback);
        }

        void EquipCallback(NetWork.Packets.Packet packet, bool bSuccess)
        {
            Debug.Log("recv equip a equip...");
            if (bSuccess)
            {
                SCFormationOn msg = packet.kBody as SCFormationOn;
                if (msg != null)
                {
                    UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);

                    UIEquipment equip = StaticData.playerData.GetUIEquipment(leftGuid);

                    formation.UpdateEquip(leftGuid, equip.equipData.type);

                    PlayParticle();

                    //Close();
                }

            }
        }

        void PlayParticle()
        {
            frameRoot.SetActive(false);
            equipBefore.gameObject.SetActive(true);

            UIEquipment equip = StaticData.playerData.GetUIEquipment(leftGuid);
            List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroIdBefore, equip.equipId);
            if (weapons.Count > 0)
                equipBefore.SetPath("Equip2d/" + weapons[0].pngRes);
            else
                equipBefore.SetPath("Equip2d/" + equip.equipData.pictrue);

            equipBefore.GetComponent<UITweener>().ResetToBeginning();
            equipBefore.GetComponent<UITweener>().PlayForward();

            StopCoroutine("ShowParticle");
            StartCoroutine(ShowParticle());
        }

        IEnumerator ShowParticle()
        {
            yield return new WaitForSeconds(1f);

            if (particleObj == null)
            {
                particleObj = Instantiate(particlePref);
                particleObj.transform.parent = equipBefore.transform.parent;
                particleObj.transform.localScale = Vector3.one;
                particleObj.transform.localPosition = Vector3.zero;
            }

            particleObj.SetActive(true);
            AudioManager.Instance.PlayUIAudio("zhuangbeichongsu");

            yield return new WaitForSeconds(0.5f);

            equipBefore.gameObject.SetActive(false);

            yield return new WaitForSeconds(1f);

            UIEquipment equip = StaticData.playerData.GetUIEquipment(leftGuid);
            equipAfter.gameObject.SetActive(true);
            List<TableWeaponRes> weapons1 = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroIdAfter, equip.equipId);
            if (weapons1.Count > 0)
                equipAfter.SetPath("Equip2d/" + weapons1[0].pngRes);
            else
                equipAfter.SetPath("Equip2d/" + equip.equipData.pictrue);
            
            UITweener[] list = equipAfter.transform.GetComponents<UITweener>();
            for (int j = 0; j < list.Length; j++)
            {
                list[j].ResetToBeginning();
                list[j].PlayForward();
            }
            yield return new WaitForSeconds(1.8f);
            
            Close();
        }

        public void OnCheckBox()
        {
            if(checkBox.value)
            {
                EquipSimplePage.dontShowRemodel = true;
            }
            else
            {
                EquipSimplePage.dontShowRemodel = false;
            }
        }

        override protected void DoOpen()
        {
            frameRoot.SetActive(true);
            equipBefore.gameObject.SetActive(false);
            equipAfter.gameObject.SetActive(false);
            equipAfter.transform.localPosition = Vector3.zero;
            equipAfter.transform.localScale = Vector3.one;
            
            if (particleObj != null)
                particleObj.SetActive(false);

            leftGuid = options["guid"];
            heroPosition = int.Parse(options["heroPosition"]);

            UIHeroFormation formation = StaticData.playerData.InstPlayerData.GetBattleCamp(heroPosition);
            UIEquipment equip = StaticData.playerData.InstPlayerData.GetUIEquipment(leftGuid);

            heroIdBefore = equip.lastHeroId;
            heroIdAfter = formation.hero.heroId;

            if (leftIcon == null)
            {
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                GameObject icon = Instantiate(obj);
                leftIcon = icon.GetComponent<EquipIcon>();
                icon.transform.parent = leftRoot.transform;
                icon.transform.localPosition = new Vector3(0, 0, 0);
                icon.transform.localScale = new Vector3(1,1,1);

                GameObject icon2 = Instantiate(obj);
                rightIcon = icon2.GetComponent<EquipIcon>();
                icon2.transform.parent = rightRoot.transform;
                icon2.transform.localPosition = new Vector3(0, 0, 0);
                icon2.transform.localScale = new Vector3(1, 1, 1);
            }

            leftIcon.SetEquip(equip);
            rightIcon.SetEquip(equip,formation.hero.heroId);

            leftEquipName.text = equip.GetWeaponNameWithHero(equip.lastHeroId); //TableTextBlo.GetText("name_" + equip.equipId);
            rightEquipName.text = equip.GetWeaponNameWithHero(formation.hero.heroId); //TableTextBlo.GetText("name_" + equip.equipId);
        }

        override protected void DoClose()
        {

        }
    }

}
